package com.cloudkinto.appletservice.covershows.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloudkinto.appletservice.covershows.AppletVideoInformationService;
import com.cloudkinto.appletservice.covershows.vo.AppletVideoInfoDetailRes;
import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.entity.CustomerCaseDo;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * service实现类
 * </p>
 *
 * @author zhangyy
 * @since 2020-10-12
 */
@Service
@Transactional(rollbackFor = Exception.class)//事务回滚
public class AppletVideoInfoServiceImpl implements AppletVideoInformationService {
    List<String> videoList = new ArrayList<>();

//    @Override
//    public AppletVideoInfoDetailRes detail(Long id) {
//        AppletVideoInfoDetailRes res = new AppletVideoInfoDetailRes();
//        BeanUtils.copyProperties(entityDo, res);
//        if(StringUtils.isNotBlank(entityDo.getAntistop())){
//            String[] split = entityDo.getAntistop().split(";");
//            res.setAntistopList(Arrays.asList(split));
//        }
//        return res;
//    }
//


    @Override
    public PageResult getListPage(Map<String, String> map) {
        videoList = new ArrayList<>();
        videoList.add("税收.mp4");
        videoList.add("关税演变史.mp4");
        videoList.add("清关公司的三个条件.m4v");
        videoList.add("菩提说1.mp4");
        videoList.add("2_菩提说2.m4v");
        videoList.add("菩提说3.m4v");
//        QueryWrapper<CustomerCaseDo> wrapper = queryBuild(map);
        IPage<CustomerCaseDo> page = pageInit(map);
//        page = this.page(page, wrapper);
        List<AppletVideoInfoDetailRes> resList = handlerListAfter(videoList);
        PageResult result = new PageResult(resList, (int) page.getCurrent(), (int) page.getSize(), (int) page.getTotal(), (int) page.getPages());
        return result;
    }


    //查询分页列表之后对列表数据的处理
    private List<AppletVideoInfoDetailRes> handlerListAfter(List<String> list) {
        List<AppletVideoInfoDetailRes> result = new ArrayList<>();
        String urlPre = "https://image.cloudkinto.com/applet/video_src/";
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(title -> {
                AppletVideoInfoDetailRes res = new AppletVideoInfoDetailRes();
                res.setTitle(title.split("\\.")[0]);
                res.setUrl(urlPre + title);
                res.setContent("");
//                BeanUtils.copyProperties(newsInformationDo, res);
//                if(StringUtils.isNotBlank(newsInformationDo.getAntistop())){
//                    String[] split = newsInformationDo.getAntistop().split(";");
//                    res.setAntistopList(Arrays.asList(split));
//                }
                result.add(res);
            });
        }
        return result;
    }

    private IPage<CustomerCaseDo> pageInit(Map<String, String> map) {
        IPage<CustomerCaseDo> page = new Page();
        page.setSize(StringUtils.isNotBlank(map.get("pageSize")) ? Long.parseLong(map.get("pageSize")) : 5);
        page.setCurrent(StringUtils.isNotBlank(map.get("current")) ? Long.parseLong(map.get("current")) : 1);
        return page;
    }

    private QueryWrapper<CustomerCaseDo> queryBuild(Map<String, String> map) {
        QueryWrapper<CustomerCaseDo> wrapper = new QueryWrapper<>();
//        if (StringUtils.isNotBlank(map.get("keyword"))) {
//            wrapper.like("keyword", map.get("keyword"));
//        }
        return wrapper.orderByDesc("id");
    }


}
